cButton = {}
cButton.Height = 0
cButton.Width = 0
cButton.X = 0
cButton.Y = 0
cButton.OnClick = nil
cButton.Image = nil
cButton.Text = ""
cButton.OnClick = nil
cButton.OnMouseUp = nil
cButton.OnMouseDown = nil
cButton.OnHoverIn = nil
cButton.OnHoverOut = nil
cButton.Visible = false

cButton.Opacity = 0
cButton.FadeIn = false
cButton.RandSeedTimer = ""
cButton.IsActive = false

function cButton:Init()
	surface.CreateFont("Paskowy", 12, 800, false, false, "ButtonText")	
	--surface.CreateFont("Visitor TT2 BRK", 12, 500, false, false, "ButtonText") -- alternate pixel font
	self.RandSeedTimer = "FadeInButton"..tostring(math.random(0,5000))
	self.FadeIn = false
	self.Opacity = 0
	self.Visible = true
	self.Text = ""
end

function cButton:SetVisible(bool)
	if not type(bool) == "boolean" then return end
	self.Visible = bool
	gui.EnableScreenClicker( self.Visible )
end


function Fade(i, ref)
	if i == 1 then
		if ref.Opacity < 24 then
			ref.Opacity = ref.Opacity + 1
		end				
	elseif i == 0 then
		if ref.Opacity > 0 then
			ref.Opacity = ref.Opacity - 1
		else
			ref.FadeIn = false
		end
	end		
	ref.Opacity = math.Clamp(ref.Opacity, 0, 24)
end

function cButton:Paint()
	if self.Visible then
		local x,y = 9,12
		
		-- draw the main rectangle
		surface.SetDrawColor( 0, 0, 0, 25 )
		surface.DrawRect( 0, 0, self.Width, self.Height )
		
		-- draw the greyish overlay on hover
		if self.FadeIn then
			surface.SetDrawColor( 135, 135, 135, self.Opacity )
			surface.DrawRect( 0, 0, self.Width, self.Height )
		end
		
		-- draw the white outline
		surface.SetDrawColor( 255, 255, 255, 120 )
		surface.DrawOutlinedRect( 0, 0, self.Width, self.Height)		
		
		-- if there is an image specified, draw it
		if self.Image then
			surface.SetMaterial(Material(self.Image))
			surface.DrawTexturedRect(x, y-2, 16, 16)
			x = x + 20
		end
		
		-- draw the white text with shadow
		surface.SetFont("ButtonText")		
		surface.SetTextColor( 0, 0, 0, 255 )
		surface.SetTextPos(x,y)
		surface.DrawText(self.Text)
		surface.SetTextColor( 255, 255, 255, 255 )
		surface.SetTextPos(x+1,y)
		surface.DrawText(self.Text)
		
	end
	return true
end

function cButton:Register(w,h,x,y)
	self.Width = w
	self.Height = h
	self.X = x
	self.Y = y
end

function cButton:SetImage(image)
	self.Image = image
end

function cButton:SetText(txt)
	self.Text = txt
end

function cButton:SetActive(bool)
	timer.Destroy(self.RandSeedTimer)
	self.IsActive = bool
	if bool then self.Opacity = 96 else self.Opacity = 0 end
end

function cButton:OnMousePressed( m )
	self.OnClick()
	self:SetActive(true)
	if ( self.OnMouseDown != nil ) then
		self.OnMouseDown( m );
	end	
	surface.PlaySound("buttons/lightswitch2.wav")
end

function cButton:OnMouseReleased( m )
	if ( self.OnMouseUp != nil ) then
		self.OnMouseUp( m );
	end
end

function cButton:OnCursorEntered()
	if not self.IsActive then
		self.FadeIn = true
		timer.Create(self.RandSeedTimer, 0.02, 24, Fade, 1, self)	
	end
end

function cButton:OnCursorExited()
	if not self.IsActive then
		self.FadeIn = true
		timer.Create(self.RandSeedTimer, 0.02, 24, Fade, 0, self)	
	end
end
vgui.Register("cButton", cButton, "Panel")